home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / sgml / msdos / sgml07 / unixproc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-30  |  1.8 KB  |  97 lines

  1. /* unixproc.c -
  2.  
  3.    Unix implementation of run_process().
  4.  
  5.      Written by James Clark (jjc@jclark.com).
  6. */
  7.  
  8. #include "config.h"
  9.  
  10. #ifdef SUPPORT_SUBDOC
  11.  
  12. #include "std.h"
  13. #include "entity.h"
  14. #include "appl.h"
  15.  
  16. #ifdef POSIX
  17.  
  18. #include <unistd.h>
  19. #include <sys/types.h>
  20. #include <sys/wait.h>
  21.  
  22. #else /* not POSIX */
  23.  
  24. #define WIFSTOPPED(s) (((s) & 0377) == 0177)
  25. #define WIFSIGNALED(s) (((s) & 0377) != 0 && ((s) & 0377 != 0177))
  26. #define WIFEXITED(s) (((s) & 0377) == 0)
  27. #define WEXITSTATUS(s) (((s) >> 8) & 0377)
  28. #define WTERMSIG(s) ((s) & 0177)
  29. #define WSTOPSIG(s) (((s) >> 8) & 0377)
  30. #define _SC_OPEN_MAX 0
  31. #define sysconf(name) (20)
  32. typedef int pid_t;
  33.  
  34. #endif /* not POSIX */
  35.  
  36. #ifndef HAVE_VFORK
  37. #define vfork() fork()
  38. #endif /* not HAVE_VFORK */
  39.  
  40. #ifdef HAVE_VFORK_H
  41. #include <vfork.h>
  42. #endif /* HAVE_VFORK_H */
  43.  
  44. int run_process(argv)
  45. char **argv;
  46. {
  47.      pid_t pid;
  48.      int status;
  49.      int ret;
  50.  
  51.      /* Can't trust Unix implementations to support fflush(NULL). */
  52.      fflush(stderr);
  53.      fflush(stdout);
  54.  
  55.      pid = vfork();
  56.      if (pid == 0) {
  57.       /* child */
  58.       int i;
  59.       int open_max = (int)sysconf(_SC_OPEN_MAX);
  60.  
  61.       for (i = 3; i < open_max; i++)
  62.            (void)close(i);
  63.       execvp(argv[0], argv);
  64.       appl_error(E_EXEC, argv[0], strerror(errno));
  65.       fflush(stderr);
  66.       _exit(127);
  67.      }
  68.      if (pid < 0) {
  69.       appl_error(E_FORK, strerror(errno));
  70.       return -1;
  71.      }
  72.      /* parent */
  73.      while ((ret = wait(&status)) != pid)
  74.       if (ret < 0) {
  75.            appl_error(E_WAIT, strerror(errno));
  76.            return -1;
  77.       }
  78.      if (WIFSIGNALED(status)) {
  79.       appl_error(E_SIGNAL, argv[0], WTERMSIG(status));
  80.       return -1;
  81.      }
  82.      /* Must have exited normally. */
  83.      return WEXITSTATUS(status);
  84. }
  85.  
  86. #endif /* SUPPORT_SUBDOC */
  87.  
  88. /*
  89. Local Variables:
  90. c-indent-level: 5
  91. c-continued-statement-offset: 5
  92. c-brace-offset: -5
  93. c-argdecl-indent: 0
  94. c-label-offset: -5
  95. End:
  96. */
  97.